/* Implementation of the ServerSocket class */

#include "ServerSocket.h"
#include "SocketException.h"


ServerSocket::ServerSocket ( int port )
{
	if ( ! Socket::create4() )
	{
		throw SocketException ( "Could not create server socket." );
	}

	if ( ! Socket::bind4 ( port ) )
	{
		throw SocketException ( "Could not bind to port." );
	}

	if ( ! Socket::listen() )
	{
		throw SocketException ( "Could not listen to socket." );
	}

}

ServerSocket::~ServerSocket()
{
}


const ServerSocket& ServerSocket::operator << ( const std::string& s ) const
{
	if ( ! Socket::send ( s ) )
	{
		throw SocketException ( "Could not write to server socket." );
	}

	return *this;

}


const ServerSocket& ServerSocket::operator >> ( std::string& s ) const
{
	int tmp;
	try {
	if ( ! (tmp = Socket::recv ( s )) )
	{
		//throw SocketException ( "Could not read from server socket." );
	}
	}
	catch (const SocketException& e)
	{
		cout << "tmp = " << tmp << endl;
		cout << "recv Exception was caught: " << e.description() << endl;
	}

	return *this;
}

void ServerSocket::accept ( ServerSocket& sock )
{
	if ( ! Socket::accept ( sock ) )
	{
		throw SocketException ( "Could not accept client socket." );
	}
}

